﻿// bsn CommandLine Library
// -----------------------
// 
// Copyright 2014 by Arsène von Wyss - avw@gmx.ch
// 
// Development has been supported by Sirius Technologies AG, Basel
// 
// Source:
// 
// https://bsn-commandline.googlecode.com/hg/
// 
// License:
// 
// The library is distributed under the GNU Lesser General Public License:
// http://www.gnu.org/licenses/lgpl.html
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//  
using System;
using System.Diagnostics;
using System.Text;

using bsn.GoldParser.Semantic;

namespace bsn.CommandLine.Parser {
	[Terminal("QuotedLiteral")]
	public class QuotedLiteralToken: LiteralToken {
		internal static string UnquoteLiteral(string value) {
			StringBuilder result = new StringBuilder(value.Length-2);
			Debug.Assert(value[0] == '"');
			int i = 1;
			bool keepQuote = false;
			while (i < value.Length) {
				char c = value[i++];
				bool isNotQuote = c != '"';
				if (isNotQuote || keepQuote) {
					result.Append(c);
					keepQuote = false;
				} else {
					keepQuote = true;
				}
			}
			return result.ToString();
		}

		public QuotedLiteralToken(string value): base(UnquoteLiteral(value)) {}
	}
}
